package com.hunan.api.catv.service.biz.member.login;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.LoginType;
import com.hunan.api.catv.entity.enums.SmsTypeEnum;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.entity.vo.SystemDictVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.apple.AppleBiz;
import com.hunan.api.catv.service.biz.member.LoginService;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.sms.SmsBiz;
import com.hunan.api.catv.service.impl.member.MemberService;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.MessageFormat;
import java.util.Optional;

@Service
public class AppleLoginService extends ResultService implements LoginService {

    @Autowired
    MemberService memberService;

    @Autowired
    AppleBiz appleBiz;

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    SmsBiz smsBiz;

    @Autowired
    SystemDictBiz systemDictBiz;

    @Override
    public ServiceResult login(String id, String phone, String sms, String ip, String pushToken, String... extParams) {
        if (!ObjectUtils.isEmpty(id)) {
            String appleUserInfoKey = MessageFormat.format(CacheKey.member_appleUserInfo, id);
            JsonObject memberJson = new JsonObject();
            MemberVo memberVo = new MemberVo();
            if (ObjectUtils.isEmpty(redisUtils.get(appleUserInfoKey))) {
                ServiceResult verifyResult = appleBiz.verify(id);
                if (verifyResult.status) {
                    ServiceResult userResult = appleBiz.getUserInfo(id);
                    if (userResult.getStatus()) {
                        JsonObject info = GsonUtils.fromJson(userResult.getData().toString(), JsonObject.class);
                        String sub = info.get("sub").getAsString();
                        memberVo = memberBiz.infoById(LoginType.apple.getCode(), sub);
                        if (ObjectUtils.isEmpty(memberVo)) {
                            redisUtils.set(appleUserInfoKey, sub, 7200);
                            memberJson.addProperty("isBind", true);
//                            memberJson.add("member",null);
                            return buildOK(memberJson, "");
                        }
                        memberJson.addProperty("isBind", false);
                        memberJson.addProperty("member", GsonUtils.toJson(memberVo));
                        return buildOK(memberJson, "");
                    }
                }
            }
            if (!ObjectUtils.isEmpty(phone) && !ObjectUtils.isEmpty(sms)) {
                ServiceResult checkSmsResult = smsBiz.checkSmsCode(SmsTypeEnum.BOUND.getCode(), phone, sms);

                //TODO 苹果测试通过后删除这个if判断
                //-------------------------------------------
                Boolean testSwitch = Boolean.valueOf(Optional.ofNullable(systemDictBiz.info("AppleTestLoginSwitch")).orElse(new SystemDictVo()).getDictValue());
                if (testSwitch) {
                    if ("123456".equals(sms) && "15675882044".equals(phone)) {
                        checkSmsResult = buildOK(true);
                    }
                }
                //-------------------------------------------
                if (!checkSmsResult.getStatus()) {
                    return checkSmsResult;
                }
                //验证该手机号是否被绑定
                ServiceResult checkPhoneResult = memberBiz.checkPhoneId(LoginType.apple.getCode(), phone, id);
                if (!checkPhoneResult.getStatus()) {
                    return checkPhoneResult;
                }
                //绑定/注册
                ServiceResult boundResult = memberBiz.boundPhone(LoginType.apple.getCode(), id, phone);
                if (!boundResult.getStatus()) {
                    return boundResult;
                }
                memberJson.addProperty("isBind", false);
                memberVo = memberBiz.getByPhone(phone);
                memberJson.addProperty("member", GsonUtils.toJson(memberVo));
                redisUtils.del(MessageFormat.format(CacheKey.member_wxUserInfo, id));
                return buildOK(memberJson, "");
            }
        }
        return buildFail(false, ConstantsMsg.appleld_auth_fail);
    }

    @Override
    public ServiceResult bind(String id, Integer loginType, Integer memberId) {
        // TODO Auto-generated method stub
        return null;
    }


}